/**
 * ReportGear(2011)
 */
package net.abbp.report.designer.components.resume;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.abbp.report.model.ReportModel;
import net.abbp.report.model.io.impl.ModelXmlIO;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.report.core.api.idname.DefaultIdName;
import com.reportgear.report.core.api.idname.IdName;

/**
 * 简历报表模板
 * 
 * @version 1.0 2011-4-17
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ResumeTemplateList extends JPanel {
    private static final long serialVersionUID = 5617250026217360560L;
    private JList reports;
    private DefaultListModel model;

    public ResumeTemplateList() {
        this.initComponents();
        this.initReports();
    }

    private void initComponents() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(200, 20));

        JLabel title = new JLabel("简历模板列表", JLabel.CENTER);
        title.setPreferredSize(new Dimension(200, 32));
        title.setBorder(BorderFactory.createEtchedBorder());
        this.add(title, BorderLayout.NORTH);

        this.model = new DefaultListModel();
        this.reports = new JList(this.model);
        this.reports.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.reports.setFixedCellHeight(24);
        this.reports.addListSelectionListener(new ListSelectionListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void valueChanged(ListSelectionEvent evt) {
                IdName<Integer> selected = (IdName<Integer>) ((JList) evt.getSource()).getSelectedValue();
                if (!evt.getValueIsAdjusting() && (selected != null)) {
                    ResumeTemplateList.this.firePropertyChange("CurrModel", null, selected);
                }
            }
        });

        this.add(new JScrollPane(this.reports), BorderLayout.CENTER);
    }

    /**
     * 初始化所有报表
     */
    private void initReports() {
        List<SalaryDataObject> templateList = SalaryHelper.INSTANCE.loadResumeTemplateList();

        for (SalaryDataObject sdo : templateList) {
            this.model.addElement(new DefaultIdName<Integer>(sdo.getInt("id"), sdo.getString("name")));
        }
    }

    /**
     * 新建报表
     * 
     * @param name
     *            报表名称
     */
    public void newReport(String name) {
        ReportModel model = new ReportModel();
        String modelXml = (new ModelXmlIO()).write(model);
        int id = SalaryHelper.INSTANCE.saveResumeTemplate(modelXml, 0, name, 1);

        if (id != -1) {
            IdName<Integer> nr = new DefaultIdName<Integer>(id, name);
            this.model.addElement(nr);
            this.reports.setSelectedIndex(this.model.getSize() - 1);
        }
    }

    /**
     * 删除模板
     */
    @SuppressWarnings("unchecked")
    public void deleteReport() {
        IdName<Integer> selected = (IdName<Integer>) this.reports.getSelectedValue();

        if (selected != null) {
            if (SalaryHelper.INSTANCE.deleteResumeTemplate(selected.getId())) {
                this.model.removeElement(selected);
                this.selectFirst();
            }
        }
    }

    public void selectFirst() {
        this.reports.setSelectedIndex(0);
    }
}
